package person.aoao.library.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import person.aoao.library.dao.BannerListDao;
import person.aoao.library.entity.BannerList;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.BannerListService;
import person.aoao.library.vo.ListData;

import javax.annotation.Resource;
import java.util.List;

/**
 * (BannerList)表服务实现类
 *
 * @author makejava
 * @since 2023-05-05 10:15:42
 */
@Service("bannerListService")
public class BannerListServiceImpl implements BannerListService {
    @Resource
    private BannerListDao bannerListDao;

    /**
     * 通过ID查询单条数据
     *
     * @param lid 主键
     * @return 实例对象
     */
    @Override
    public BannerList queryById(Integer lid) {
        return this.bannerListDao.queryById(lid);
    }

    /**
     * 分页查询
     *
     * @param bannerList  筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<BannerList> queryByPage(BannerList bannerList, PageRequest pageRequest) {
        long total = this.bannerListDao.count(bannerList);
        return new PageImpl<>(this.bannerListDao.queryAllByLimit(bannerList, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param bannerList 实例对象
     * @return 实例对象
     */
    @Override
    public BannerList insert(BannerList bannerList) {
        this.bannerListDao.insert(bannerList);
        return bannerList;
    }

    /**
     * 修改数据
     *
     * @param bannerList 实例对象
     * @return 实例对象
     */
    @Override
    public BannerList update(BannerList bannerList) {
        this.bannerListDao.update(bannerList);
        return this.queryById(bannerList.getLid());
    }

    /**
     * 通过主键删除数据
     *
     * @param lid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer lid) {
        return this.bannerListDao.deleteById(lid) > 0;
    }

    @Override
    public ResponseData getBanner(Integer num,Integer page) {
        ListData list=new ListData();
        long count=this.bannerListDao.selectCount();
        list.setCount(count);
        List<BannerList> data=this.bannerListDao.getBanner(num,(page-1)*num);
        list.setData(data);
        return new ResponseData(ResponseCode.SUCCESS,list);
    }

    @Override
    public ResponseData addBanner(BannerList bannerList) {
        Integer num=this.bannerListDao.insert(bannerList);
        if(num>0){
            return  new ResponseData(ResponseCode.SUCCESS);
        }else{
            return new ResponseData(ResponseCode.FIAL);
        }
    }

    @Override
    public ResponseData editBanner(BannerList bannerList) {
        Integer num=this.bannerListDao.update(bannerList);
        if(num>0){
            return  new ResponseData(ResponseCode.SUCCESS);
        }else{
            return new ResponseData(ResponseCode.FIAL);
        }
    }

    @Override
    public ResponseData deleteBanner(Integer lid) {
        Integer num=this.bannerListDao.deleteById(lid);
        if(num>0){
            return  new ResponseData(ResponseCode.SUCCESS);
        }else{
            return new ResponseData(ResponseCode.FIAL);
        }
    }

    @Override
    public ResponseData searchBanner(String value, String state,Integer num, Integer page) {
        System.out.println(value);
        System.out.println(state);
        ListData data=new ListData();
        long count =this.bannerListDao.searchCount('%'+value+"%",state);
        List<BannerList> list=this.bannerListDao.searchBanner('%'+value+"%",state,num,(page-1)*num);
        data.setCount(count);
        data.setData(list);
        return new ResponseData(ResponseCode.SUCCESS,data);
    }

    @Override
    public ResponseData getBannerOnApp() {
        List<BannerList> data=this.bannerListDao.getBannerOnApp();
        return new ResponseData(ResponseCode.SUCCESS,data);
    }
}
